<?php
class advertiseModel extends Model{
	private $tb = 'advertise_picture';

	public function __construct(){
		parent::__construct($this->tb);
	}
	
	private function _valid( &$data ){
		if( empty($data['file_name']) || empty($data['type']) ){
			$this->error( lang( array('required_fields','required') ) );	
		}elseif( !valid_str_len($data['file_name'],120) ){
			$this->error( lang( array('file','name','len_error') ) );	
		}
		return true;
	}

	public function get_list( $where="", $advertise_type=1 ){
		$strWhere = "type=$advertise_type";
		if( !empty($where) ){
			$strWhere .= " AND $where";
		}
		$this->free_result();
		return $this->where($strWhere)->order('`order`')->get();
	}

	public function add_adv( $data ){
		if( !$this->_valid($data) ){
			return false;
		}
		$data['order'] = empty( $data['order'] ) ? 0 : intval($data['order']);
		return $this->value($data)->add();
	}

	public function edit_adv( $id, $data ){
		if( empty($id) || empty($data) ){
			return false;
		}
		unset($data['id']);
		return $this->where("id=".intval($id))->value($data)->update();
	}

	public function del_adv($id){
		$id = intval($id);
		if( empty($id) ){
			return false;
		}
		return $this->where("id=$id")->del();
	}
}	

